﻿using HomeWork5;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HomeWork8_WinFrom
{
    public partial class OrderModifyForm : Form, INotifyPropertyChanged
    {
        public int _productNumber;
        public string _productName;
        public string _clientName;
        public double _productPrice;
        public double _totalMoney;
        public int ProductNumber
        {
            get => _productNumber;
            set
            {
                if (_productNumber != value)
                {
                    _productNumber = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("ProductNumber"));
                }
            }
        }
        new public string ProductName
        {
            get => _productName;
            set
            {
                if (_productName != value)
                {
                    _productName = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("ProductName"));
                }
            }
        }
        public string ClientName
        {
            get => _clientName;
            set
            {
                if (_clientName != value)
                {
                    _clientName = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("ClientName"));
                }
            }
        }
        public double ProductPrice
        {
            get => _productPrice;
            set
            {
                if (_productPrice != value)
                {
                    _productPrice = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("ProductPrice"));
                }
            }
        }
        public double TotalMoney
        {
            get => _totalMoney;
            set
            {
                if (_totalMoney != value)
                {
                    _totalMoney = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("TotalMoney"));
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public List<OrderItem> orderItems = new List<OrderItem>();
        public Order order = new Order();
        public OrderModifyForm()
        {
            InitializeComponent();
        }
        public OrderModifyForm(Order order) : this()
        {
            this.order =order;
        }
        private void OrderModifyForm_Load(object sender, EventArgs e)
        {
            txtProductNumber.DataBindings.
               Add("Text", this, "ProductNumber");
            txtClientName.DataBindings.
                Add("Text", this, "ClientName");
            cbxProductName.DataBindings.
                Add("SelectedItem", this, "ProductName");
            lblProductPrice.DataBindings.
                Add("Text", this, "ProductPrice");
            lblTotalMoney.DataBindings.
                Add("Text", this, "TotalMoney");
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            lblOrderItem.Text = "";
            OrderItem orderItem =
                new OrderItem(new Product(ProductName, ProductPrice), ProductNumber);
            order.AddOrderItem(orderItem);
            TotalMoney += ProductPrice * ProductNumber;
            order.OrderItems.ForEach(o => lblOrderItem.Text += o + " ");
        }

        private void cbxProductName_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProductName = "";
            switch (cbxProductName.SelectedIndex)
            {
                case 0:

                    ProductPrice = 5;
                    break;
                case 1:

                    ProductPrice = 3;
                    break;
                case 2:

                    ProductPrice = 6;
                    break;
                case 3:

                    ProductPrice = 4;
                    break;
                case 4:

                    ProductPrice = 10;
                    break;
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            order.Client = new Client(ClientName);
            
            Close();
        }
    }
}
